package com.itheima.controller;

import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.service.CheckGroupSrvice;
import com.itheima.pojo.CheckGroup;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkgroup")
public class CheckgroupContronller {


    @Reference
    private CheckGroupSrvice checkGroupSrvice;


    //添加数据
    @RequestMapping("add.do")
    public Result add(@RequestBody CheckGroup checkGroup,@RequestParam("checkitemIds") Integer[] checkitemIds){
        try {
            checkGroupSrvice.add(checkGroup,checkitemIds);
            return  new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }
    //分页查询
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {

            Map map = checkGroupSrvice.findPageByCondition(queryPageBean);



            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @RequestMapping("deleteCheckGroupitemById.do")
    public Result delectByid(@RequestParam("id") Integer id){

        try {
            CheckGroup checkGroup = new CheckGroup();
            checkGroup.setId(id);
            checkGroup.setIs_delete(1);

            checkGroupSrvice.updateById(checkGroup);

            return new Result(true,MessageConstant.UPDATE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKGROUP_FAIL);
        }

    }


    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findAll.do")
    public Result findAll(){
        try {
            List<CheckGroup> list = checkGroupSrvice.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
}
